package com.tg.app.activity.device.add;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.demo.view.PopupWindowHelper;
import com.tg.app.R;
import com.tg.app.helper.AppHelper;
import com.tg.app.listener.OnAddDeviceListener;
import com.tg.appcommon.android.DeviceAddSoundConstants;
import com.tg.appcommon.android.SoundPlayerManager;


public class AddGuideFragment extends Fragment {

    private TextView tvDescription,tvTips,apLocalTipsTextView;
    private  int originType;
    private CheckBox  cbListener;
    private OnAddDeviceListener listener;
    private Button btnNext;
    private PopupWindowHelper mPopupWindowHelper;

    private boolean playSound = false;

    private SoundPlayerManager soundPlayerManager;

    private final Handler uiHandler = new Handler(Looper.getMainLooper());

    public AddGuideFragment() {
        // Required empty public constructor
    }


    public static AddGuideFragment newInstance(int param) {
        AddGuideFragment fragment = new AddGuideFragment();
        Bundle args = new Bundle();
        args.putInt(AddFragmentTabActivity.EXT_DEVICE_ADD_TYPE, param);
        fragment.setArguments(args);
        return fragment;
    }
    public void setListener(OnAddDeviceListener listener) {
        this.listener = listener;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            originType = getArguments().getInt(AddFragmentTabActivity.EXT_DEVICE_ADD_TYPE,0);
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (originType== AddFragmentTabActivity.DEVICE_ADD_TYPE_QRCODE){
            playSound = true;
            tvDescription.setText(R.string.device_add_qrcode_turn_on_power);
            tvTips.setVisibility(View.VISIBLE);
            tvTips.setOnClickListener(new View.OnClickListener() {
                @Override

                public void onClick(View v) {
                    showTipsPop();
                }
            });
        }else if  (originType== AddFragmentTabActivity.DEVICE_ADD_TYPE_AP){
            tvDescription.setText(R.string.device_add_ap_turn_on_power);
        }else if  (originType== AddFragmentTabActivity.DEVICE_ADD_TYPE_LAN){
            tvDescription.setText(R.string.preparation_before_add_lan_description);
        }else if (originType == AddFragmentTabActivity.DEVICE_ADD_TYPE_AP_LOCAL){
            tvDescription.setText(R.string.device_add_ap_turn_on_power);
            apLocalTipsTextView.setVisibility(View.VISIBLE);
        }else if (originType == AddFragmentTabActivity.DEVICE_ADD_TYPE_QRCODE_SCAN){
            tvDescription.setText(R.string.device_add_ap_turn_on_power);
            btnNext.setEnabled(false);
            cbListener.setVisibility(View.VISIBLE);
        }

        if (AppHelper.isDuomigeApp()){
            AppHelper.setDuoigeText(tvDescription, cbListener, tvTips);
        }
    }

    @Override
    public void onResume() {
        super.onResume();

        playSound(DeviceAddSoundConstants.WifiPower(getActivity()));
    }

    private void playSound(String url) {
        if (!playSound) return;
        if (soundPlayerManager != null) {
            soundPlayerManager.destroy();
        }
        if (getActivity() == null) return;
        soundPlayerManager = SoundPlayerManager.create(getActivity());
        uiHandler.postDelayed(() -> {
            if (getActivity() == null) return;
            if (soundPlayerManager != null) {
                soundPlayerManager.download(url, true);
            }
        }, 1000);
    }

    @Override
    public void onPause() {
        super.onPause();
        pauseSound();
    }

    private void pauseSound() {
        if (!playSound) return;
        uiHandler.removeCallbacksAndMessages(null);
        if (soundPlayerManager != null) {
            soundPlayerManager.destroy();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View  view =  inflater.inflate(R.layout.fragmen_add_guide, container, false);
        tvDescription  = view.findViewById(R.id.tv_preparation_before_add_description);
        tvTips = view.findViewById(R.id.tips);
        apLocalTipsTextView = view.findViewById(R.id.ap_local_tips);
        cbListener = view.findViewById(R.id.cb_listener);
        btnNext = view.findViewById(R.id.btn_before_add_next);
        btnNext.setOnClickListener(view1 -> {
                if (listener!=null){
                listener.onClickBtnForDevice();
            }
        });
        cbListener.setOnCheckedChangeListener((compoundButton, b) -> btnNext.setEnabled(b));
        return view;
    }

    /**
     * 本地直连相关 end
     **/
    private void showTipsPop() {
        if (playSound) {
            playSound(DeviceAddSoundConstants.WifiReset(getActivity()));
        }
        mPopupWindowHelper = new PopupWindowHelper() {
            @Override
            protected void initView(@NonNull View view) {
                TextView oneTitle = view.findViewById(R.id.tv_ap_tips_setup_one_title);
                TextView twoTitle = view.findViewById(R.id.tv_ap_tips_setup_two_title);
                TextView threeTitle = view.findViewById(R.id.tv_ap_tips_setup_three_title);
                TextView oneContent = view.findViewById(R.id.tv_ap_tips_setup_one_content);
                TextView twoContent = view.findViewById(R.id.tv_ap_tips_setup_two_content);
                oneTitle.setText(R.string.qr_code_tips_one_title);
                twoTitle.setText(R.string.qr_code_tips_two_title);
                threeTitle.setText(R.string.qr_code_tips_three_title);
                oneContent.setText(R.string.qr_code_tips_one_content);
                twoContent.setText(R.string.qr_code_tips_two_content);
            }

            @Override
            protected void setTouchable() {
                mPopupWindowHelper.setTouchableImpl(true, false, false);
            }

            @SuppressLint("ClickableViewAccessibility")
            @Override
            protected void setPopupWindowListener(@NonNull View view) {
                mPopupWindowHelper.setOnDismissListener(() -> mPopupWindowHelper.setOldAttributes(getActivity()));
                view.findViewById(R.id.add_close).setOnClickListener(v -> mPopupWindowHelper.dismissPopupWindow());
                mPopupWindowHelper.getPopupWindow().setTouchInterceptor((v, event) -> {
                    return false;
                    // 这里如果返回true的话，touch事件将被拦截
                    // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
                });
            }
        };
        mPopupWindowHelper.showPopupWindow(getActivity(), R.layout.ap_add_tip_pop, getView(), Gravity.CENTER);
    }

}
